package team.zero.lemon.common.security.session;

import team.zero.lemon.common.config.Global;
import team.zero.lemon.common.security.cache.CacheManager;
import team.zero.lemon.common.security.session.model.Session;
import team.zero.lemon.common.utils.IdGen;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Session管理类
 * Created by wangbolin on 16/03/09.
 */
public class SessionManager {

    private Logger logger = LoggerFactory.getLogger(getClass());
    private static final String SESSION_KEK_TOKEN = "SESSION_KEK_TOKEN_";
    public static final int sessionTimeout = Integer.parseInt(Global.getConfig("session.sessionTimeout"));
    private static SessionManager instance;
    private static ThreadLocal<String> currentSessionToken = new ThreadLocal<String>();

    private SessionManager() {

    }

    /**
     * SessionManager对象实例（单例）
     * Created by wangbolin on 16/03/09.
     */
    public static SessionManager getIstance() {
        if (instance == null) {
            synchronized (SessionManager.class) {
                if (instance == null) {
                    instance = new SessionManager();
                }
            }
        }
        return instance;
    }

    /**
     * 设置当前会话的Token
     * Created by wangbolin on 16/03/09.
     */
    public void SetCurrentSessionToken(String token) {
        currentSessionToken.set(token);
    }

    /**
     * 获取当前会话Token
     * Created by wangbolin on 16/03/09.
     */
    public String GetCurrentSessionToken() {
        return currentSessionToken.get();
    }

    /**
     * 获取当前会话用户信息
     * Created by wangbolin on 16/03/09.
     */
    public Session GetCurrentUserSessionInfo() {
        String token = GetCurrentSessionToken();
        return get(token);
    }

    public Session GetUserSessionInfoByToken(String token) {
        return get(token);
    }

    public String add(Session session) {
        String token = IdGen.uuid();
        session.setToken(token);
        CacheManager.getInstance().put(SESSION_KEK_TOKEN + session.getToken(), session, sessionTimeout);
        return token;
    }

    /**
     *
     * @param session
     * @param timeout -1永不过期，0默认值，其它自定义
     * @return
     */
    public String add(Session session, int timeout) {
        String token = IdGen.uuid();
        session.setToken(token);
        if (timeout == -1) {
            CacheManager.getInstance().put(SESSION_KEK_TOKEN + session.getToken(), session);
        } else if (timeout == 0) {
            CacheManager.getInstance().put(SESSION_KEK_TOKEN + session.getToken(), session, sessionTimeout);
        } else if (timeout == 0) {
            CacheManager.getInstance().put(SESSION_KEK_TOKEN + session.getToken(), session, timeout);
        }
        return token;
    }

    public void remove(String token) {
        Session session = get(token);
        if (session != null) {
            CacheManager.getInstance().remove(SESSION_KEK_TOKEN + token);
        }
    }

    public Session get(String token) {
        return (Session) CacheManager.getInstance().get(SESSION_KEK_TOKEN + token, sessionTimeout);
    }
}
